//
//  HMNetworkTools.swift
//  03-Swift网络框架的封装
//
//  Created by itcast on 17/3/20.
//  Copyright © 2017年 itcast. All rights reserved.
//

import UIKit

//声明枚举
enum HTTPMethod: Int {
    case GET = 0
    case POST
}

// swift 调用OC  先添加桥接文件 再在桥接文件中导入需要使用的类的头文件
//但是如果通过cocoapods管理的第三方框架 只需要导入命名空间即可
import AFNetworking

class ZNetworkTools: AFHTTPSessionManager {
    //单例对象
    static let shared: ZNetworkTools = {
        let tools = ZNetworkTools()
        
        //设置支持的解析数据的类型
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
    }()
    
    
    
    /*
     - (void)request:(HTTPMethod)method url:(NSString *)urlString paramters:(id)paramters finished:(void (^)(id, NSError *))finished
     */
    //封装网络请求的核心方法
    func request(method: HTTPMethod,urlString: String, paramters: Any?, finished: @escaping (Any?, Error?) -> ()) {
        
        //定义闭包对象
        let success = { (task: URLSessionDataTask,responseObject: Any?) -> () in
            
            finished(responseObject, nil)
        }
        
        let failure = { (task: URLSessionDataTask?, error :Error) -> () in
            //执行失败的回调
            print(error)
            finished(nil, error)
        }
        if method == .GET {
            //get请求
            get(urlString, parameters: paramters, progress: nil, success: success, failure: failure)
        } else {
            //post 请求
            post(urlString, parameters: paramters, progress: nil, success: success, failure: failure)
        }
    }
}

//  Home请求数据
extension ZNetworkTools{
    
    func homeLoadData(since_id: Int64,max_id: Int64,finished: @escaping (Any?, Error?) -> ()){
        
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"

        let para = [
            "access_token": ZUserAccountViewModel.shareUserAccoutn.account?.access_token ?? "",
            "since_id": "\(since_id)",
            "max_id": "\(max_id)"
        ] as [String : Any]
        request(method: .GET, urlString: urlString, paramters: para, finished: finished)
    }
}


//  发布微博
extension ZNetworkTools{

    
    //  发布文字微博
    func update(statuses:String,finished: @escaping (Any?, Error?) -> ()){
        
        let urlString = "https://api.weibo.com/2/statuses/update.json"
        
        let para = [
            "access_token" : ZUserAccountViewModel.shareUserAccoutn.account?.access_token,
            "status" : statuses
        ]
        
        request(method: .POST, urlString: urlString, paramters: para, finished: finished)
    }
    
    //  发布图文微博
    func upload(statuses:String,imageList:[UIImage],finished: @escaping (Any?, Error?) -> ()) {
        
        let urlString = "https://upload.api.weibo.com/2/statuses/upload.json"
        
        let para = [
            "access_token" : ZUserAccountViewModel.shareUserAccoutn.account?.access_token,
            "status" : statuses
        ]
        
        post(urlString, parameters: para, constructingBodyWith: { (formData) in
            
            
            for image in imageList{
            
                guard let imageData = UIImagePNGRepresentation(image) else{
                    
                    print("图片转二进制失败了")
                    return
                }
                
                formData.appendPart(withFileData: imageData, name: "pic", fileName: "nihoa", mimeType: "application/octet-stream")
            }
            
        }, progress: nil, success: { (res, _) in
            
            finished(res, nil)
            
        }) { (_, error) in
            
            finished(nil,error)
        }
    }
    
}
